package com.hyj.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hyj.service.RoleModulesService;
import com.hyj.mapper.RoleModulesMapper;
import com.hyj.entity.RoleModules;

@Service
public class RoleModulesServiceImp implements RoleModulesService {
	@Autowired
	private RoleModulesMapper roleModulesmapper;

	@Override
	public List<RoleModules> selectRoleModules(List<Integer> list) {
		// TODO Auto-generated method stub
		return roleModulesmapper.selectRoleModules(list);
	}

	@Override
	public Integer addRoleModiles(String parentIds, Integer rId) {
		// TODO Auto-generated method stub
		Integer k = 0;
		String[] split = parentIds.split(",");
		List<RoleModules> modulesByRid = roleModulesmapper
				.selectRoleModulesByRId(rId);
		List<Integer> rmlist = new ArrayList<Integer>();
		for (int i = 0; i < modulesByRid.size(); i++) {
			rmlist.add(modulesByRid.get(i).getModuleId());
		}
		List<Integer> mlist = new ArrayList<Integer>();
		for (int i = 0; i < split.length; i++) {
			String string = split[i];
			int parseInt = Integer.parseInt(string);
			mlist.add(parseInt);
		}
		Integer delectRolModule = roleModulesmapper.delectRolModule(rId);
		if (delectRolModule > 0) {
			for (int i = 0; i < mlist.size(); i++) {
				RoleModules roleModules = new RoleModules();
				roleModules.setRoleId(rId);
				roleModules.setModuleId(mlist.get(i));
				k = roleModulesmapper.addRoleModules(roleModules);
			}
		} else {
			k = 0;
		}

		return k;
	}

}
